var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;
    $.removeTableCookie('SpryMedia_DataTables_jobProductTable_jobProductList.jsp');
    getDataTableJP();

    //新增
    $("#addJobProduct").click(function () {
        saveOrUpdateJobProduct(null);
    });
    // 查询
    $("#search").click(function () {
        $.removeTableCookie('SpryMedia_DataTables_jobProductTable_jobProductList.jsp');
        getDataTableJP(); //要触发的方法
        return false;
    });

    //回车事件
    $(document).keydown(function (event) {
        var code = event.which;
        if (code == 13) {
            $.removeTableCookie('SpryMedia_DataTables_jobProductTable_jobProductList.jsp');
            getDataTableJP(); //要触发的方法
            return false;
        }
    });
});
/**
 * 获取工单产品列表
 */
function getDataTableJP() {
    $('#jobProductTable').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        'fnServerParams': function (aoData) {
            var jobId = $.trim($("#jobId").val());
            var startTime = $.trim($("#startTime").val());
            var endTime = $.trim($("#endTime").val());
            aoData.push({
                "name": "jobProduct.jobId",
                "value": jobId
            }, {
                "name": "jobProduct.startTime",
                "value": startTime
            }, {
                "name": "jobProduct.endTime",
                "value": endTime
            });
        },
        "sAjaxSource": basePath + "job/getJobProductList.action", // 获取列表
        "sServerMethod": "POST",
        "sPaginationType": "full_numbers",
        "bPaginate": true, // 翻页功能
        "bStateSave": true, // 状态保存
        "bLengthChange": false, // 改变每页显示数据数量
        "bFilter": false, // 过滤功能
        "bSort": false, // 排序功能
        "bInfo": true, // 页脚信息
        "bAutoWidth": false, // 自动宽度
        "bDestroy": true,
        "iDisplayLength": 5, // 每页显示多少行
        "aoColumns": [{
            "sTitle": '排序',
            "mDataProp": "no"
        }, {
            "sTitle": '工单编号',
            "mDataProp": "jobCode"
        }, {
            "sTitle": '产品类型',
            "mDataProp": "productTypeStr"
        }, {
            "sTitle": '产品编号',
            "mDataProp": "productCode"
        }, {
            "sTitle": '产品名称',
            "mDataProp": "productName",
            "sClass": "conceal"
        }, {
            "sTitle": '出库时间',
            "mDataProp": "outTime"
        }, {
            "sTitle": '问题描述',
            "mDataProp": "problem"
        }, {
            "sTitle": '处理时间',
            "mDataProp": "dealTime"
        }, {
            "sTitle": '处理状态',
            "mDataProp": "stateStr"
        }, {
            "sTitle": '操作',
            "mDataProp": null
        }],
        "oLanguage": {
            "sUrl": basePath + "plugins/datatable/cn.txt" // 中文包
        },
        "fnDrawCallback": function (oSettings) {
            $('#jobProductTable tbody  tr td').each(function () {
                this.setAttribute('title', $(this).text());
            });
        },
        "fnInitComplete": function () {
        },
        "aoColumnDefs": [{
            "aTargets": [9],
            "fnRender": function (oObj) {
                var jpId = oObj.aData.id;
                var jpState = oObj.aData.jpState;
                if (jpState == 2) {
                    return '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-primary layui-btn-radius" onclick=detailJobProduct(\'' + jpId + '\')>查看</a>';
                } else {
                    return '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=saveOrUpdateJobProduct(\'' + jpId + '\')>处理</a>';

                }
            }
        }]
    });
}
/**
 * 查看
 * @param jpId
 */
function detailJobProduct(jpId){
    var url = "job/toUpdateOrDetail.action?type=detail&id=" + jpId;
    layer.open({
        type: 2,
        title: '查看问题产品详情',
        area: ["50%", "85%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        btn: ['关闭'],
        yes: function (index) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        zIndex: layer.zIndex,
        success: function (layero) {
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 新增/修改
 */
function saveOrUpdateJobProduct(jpId) {
    var jobId = $("#jobId").val();
    var jobCode = $("#jobCode").val();
    var url;
    var titleVal;
    if (null == jpId) {
        url = "jsp/jobProduct/addOrUpdateJobProduct.jsp?jobId=" + jobId + "&jobCode=" + jobCode;
        titleVal = "新增问题产品";
    } else {
        url = "dict/toUpdateOrDetail.action?type=update&id=" + jpId;
        titleVal = "修改问题产品";
    }
    layer.open({
        type: 2,
        title: titleVal,
        area: ["50%", "85%"],
        shade: 0.3,
        maxmin: true,
        fixed: true, //不固定
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}